import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;


public class MyVertex implements Vertex<MyVertex>{
	private String name;
	private List<Pair<MyVertex>> adjacencies;

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public MyVertex(String name)
	{
		this.name = name;
		adjacencies = new ArrayList();
	}
	
	@Override
	public String toString()
	{
		return name;
	}
	
	@Override
	public boolean equals(Object a)
	{
		boolean out = false;
		
		MyVertex compTo = (MyVertex) a;
		if(this.name.equals(compTo.getName()))
		{
			out = true;
		}
		return out;
	}
	
	public void addAdjacent(Pair<MyVertex> pair)
	{
		adjacencies.add(pair);
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List<MyVertex> buildFromFile(String filename) 
	{
		String current = "";
		int size = 0;
		ArrayList<String> first = new ArrayList<String>();
		ArrayList<String> second = new ArrayList<String>();
		ArrayList<Integer> nums = new ArrayList<Integer>();
		List<MyVertex> out = new LinkedList<MyVertex>();
		Set<MyVertex> set = new HashSet<MyVertex>();
		Set<String> strSet = new HashSet<String>();
		try 
		{
			
			Scanner scan = new Scanner(new File(filename));
			while(scan.hasNext())
			{
				current = scan.nextLine();
				StringTokenizer toke = new StringTokenizer(current);
				if(toke.hasMoreTokens())
				{
					first.add(toke.nextToken());
					second.add(toke.nextToken());
					nums.add(Integer.parseInt(toke.nextToken()));	
				}
			}
			for(int i = 0; i<first.size(); i++)
			{
				boolean firstFail = strSet.add(first.get(i));
				boolean secondFail = strSet.add(second.get(i));
				if(firstFail)	
				{
					set.add(new MyVertex(first.get(i)));
					size++;
				}
				if(secondFail)
				{
					set.add(new MyVertex(second.get(i)));
					size++;
				}
			}
			MyVertex[] arr = new MyVertex[size];
			set.toArray(arr);
			for(int i = 0; i<arr.length; i++)
			{
				out.add(arr[i]);
			}
			for(int i = 0; i<first.size();i++)
			{
				MyVertex temp = out.remove(out.indexOf(new MyVertex(first.get(i))));
				MyVertex temp2 = out.remove(out.indexOf(new MyVertex(second.get(i))));
				temp.addAdjacent(new Pair(temp2,nums.get(i)));
				temp2.addAdjacent(new Pair(temp,nums.get(i)));
				out.add(temp);
				out.add(temp2);
			}
			
		} 
		catch (FileNotFoundException e)
		{
			System.out.println(e.getMessage());
		}
		
		return out;
		
	}
	
	@Override
	public List<Pair<MyVertex>> getAdjacent() {
		return adjacencies;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Pair<MyVertex>> getAdjacencies() {
		return adjacencies;
	}
	public void setAdjacencies(List<Pair<MyVertex>> adjacencies) {
		this.adjacencies = adjacencies;
	}

}
